<template>
  <div id="addQuestionView">
    <h2>创建题目</h2>
    <a-form :model="form" label-align="left">
      <a-form-item field="title" label="标题">
        <a-input v-model="form.title" placeholder="请输入标题" />
      </a-form-item>
      <a-form-item field="tags" label="标签">
        <a-select v-model="form.tags" :style="{width:'320px'}" placeholder="请选择标签 ..." multiple allow-search :max-tag-count="4" >
          <a-option v-for="item of tagsList" :value="item.id" :label="item.text" />
        </a-select>
      </a-form-item>
      <a-form-item field="difficulty" label="难度" >
        <a-select v-model="form.difficulty" :style="{width:'320px'}" placeholder="请选择难度 ..." >
          <a-option v-for="item of difficultyList" :value="item.value" :label="item.text" />
        </a-select>
      </a-form-item>
      <a-form-item field="content" label="题目内容">
        <MdEditor :value="form.content" :handle-change="onContentChange" />
      </a-form-item>
      <a-form-item label="判题配置" :content-flex="false" :merge-props="false">
        <a-space direction="vertical" style="min-width: 480px">
          <a-form-item field="judgeConfig.timeLimit" label="时间限制">
            <a-input v-model="form.judgeConfig.timeLimit" placeholder="请输入时间限制 ..." mode="button"
              size="large" />
          </a-form-item>
          <a-form-item field="judgeConfig.memoryLimit" label="内存限制">
            <a-input v-model="form.judgeConfig.memoryLimit" placeholder="请输入内存限制 ..." mode="button"
              size="large" />
          </a-form-item>
        </a-space>
      </a-form-item>
      <a-form-item label="测试用例配置" :content-flex="false" :merge-props="false">
        <a-form-item v-for="(judgeCaseItem, index) of form.judgeCaseList" :key="index" no-style>
          <a-space direction="vertical" style="min-width: 640px">
            <a-form-item :field="`form.judgeCaseList[${index}].input`" :label="`输入用例-${index}`" :key="index">
              <a-input v-model="judgeCaseItem.input" placeholder="请输入测试输入用例" />
            </a-form-item>
            <a-form-item :field="`form.judgeCaseList[${index}].output`" :label="`输出用例-${index}`" :key="index">
              <a-input v-model="judgeCaseItem.output" placeholder="请输入测试输出用例" />
            </a-form-item>
            <a-button status="danger" @click="handleDelete(index)">
              删除
            </a-button>
          </a-space>
        </a-form-item>
        <div style="margin-top: 32px">
          <a-button @click="handleAdd" type="outline" status="success">新增测试用例
          </a-button>
        </div>
      </a-form-item>
      <a-form-item label="是否比赛用题" >
        <a-switch v-model="form.isQueried" checked-value="1" unchecked-value="0"/>
      </a-form-item>
      <a-form-item>
        <a-space>
          <a-button type="primary" style="min-width: 200px" @click="doSubmit">提交
          </a-button>
          <a-button type="primary" style="min-width: 200px" @click="toBackView">返回
          </a-button>
        </a-space>
      </a-form-item>
    </a-form>
  </div>
</template>

<script setup lang="ts">
import { onBeforeMount, onMounted, ref } from "vue";
import MdEditor from "@/components/MdEditor.vue";
import { QuestionControllerService, CommonControllerService } from "../../../generated";
import message from "@arco-design/web-vue/es/message";
import { useRoute, useRouter } from "vue-router";

const route = useRoute();
// 如果页面地址包含 update，视为更新页面
const updatePage = route.path.includes("update");

let form = ref({
  title: "",
  tags: [],
  content: "",
  difficulty: undefined,
  judgeConfig: {
    memoryLimit: 1000,
    timeLimit: 1000,
  },
  judgeCaseList: [
    {
      input: "",
      output: "",
    },
  ],
  isQueried: undefined,
});

let difficultyList = ref(
  "" as any
);

let tagsList = ref(
  "" as any
)

interface Props {
  id: string;//题目id
}

const props = withDefaults(defineProps<Props>(), {
  id: "",
});

/**
 * 根据题目 id 获取老的数据
 */
const loadData = async () => {

  const res2 = await CommonControllerService.getEnumsListUsingGet('题目难度');
  difficultyList.value = res2.data

  const res3 = await CommonControllerService.getOptionsListUsingGet('题目标签');
  tagsList.value = res3.data
  
  if(props.id){
    const res = await QuestionControllerService.getQuestionVoByIdUsingGet(
      props.id as any
    );
    if (res.code === 0) {
      form.value = res.data as any;
    } else {
      message.error("加载失败，" + res.message);
    }
  }
};

onMounted(() => {
  loadData();
});

const doSubmit = async () => {
  // 区分更新还是创建
  if (updatePage) {
    const res = await QuestionControllerService.updateQuestionUsingPost(
      form.value
    );
    if (res.code === 0) {
      message.success("更新成功");
    } else {
      message.error("更新失败，" + res.message);
    }
  } else {
    const res = await QuestionControllerService.addQuestionUsingPost(
      form.value
    );
    if (res.code === 0) {
      message.success("创建成功");
    } else {
      message.error("创建失败，" + res.message);
    }
  }
  toHomeView();
};

const router = useRouter();

const toHomeView = () => {
  router.push({
    path: `/`,
  });
};

const toBackView = () => {
  router.back();
}

/**
 * 新增判题用例
 */
const handleAdd = () => {
  form.value.judgeCaseList.push({
    input: "",
    output: "",
  });
};

/**
 * 删除判题用例
 */
const handleDelete = (index: number) => {
  form.value.judgeCaseList.splice(index, 1);
};

const onContentChange = (value: string) => {
  form.value.content = value;
};

</script>

<style scoped>
#addQuestionView {}
</style>
